﻿
@{
}

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
        body, html, #allmap {
            width: 100%;
            height: 100%;
            overflow: hidden;
            margin: 0;
            font-family: "微软雅黑";
        }
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=c4onxKnWyD0FWVU6DMu7sFF0"></script>
    <title>设置驾车路线途经点</title>
</head>
<body>
    <div id="allmap"></div>
</body>
</html>
<script type="text/javascript" src="../../Plat/Flow/lib/jquery-ui-1.8.4.custom/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
    var taskid = getQueryString("taskid");
    // 百度地图API功能
    var map = new BMap.Map("allmap");
    var geturl = "/api/OrderFormCable/GetCP_DepList";
    var longitude = 117.02782300, latitude = 40.15994800;
    var myGeo = new BMap.Geocoder();
    var index = 0, gindex = 0; var adds = new Array(); var names = new Array(); var pointArray = new Array();
    var start = new BMap.Point(longitude, latitude);
    var end;//终点
    var passPoint = new Array();//经过点，数组格式
   // debugger
    //得到信息数据
    $.getJSON(geturl, function (data) {
       // debugger
        if (data !== "null" && data.length !== 0) {
           // debugger
            data = eval('(' + data + ')');
            longitude = data[0]["longitude"];
            latitude = data[0]["latitude"];
        }
        map.centerAndZoom(new BMap.Point(longitude, latitude), 11);
        map.enableScrollWheelZoom(true);
        var url = "/api/OrderFormCable/GetTaskOrderList";
        if (taskid != "" && taskid != undefined && taskid != null) {
            //配置起止点
            $.ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(taskid),
                contentType: 'application/json',
                success: function (data) {
                   // debugger
                    if (data === "null" || data.length === 0) return;
                    //绑定
                    data = eval('(' + data + ')');
                    configMap(data);
                }
            });
        }
    });
   

    function configMap(data) {
        data = eval('(' + data + ')');
        //var start = "北京市海淀区新建宫门路19号";//起点      
        $.each(data, function (dIndex, dObj) {
            debugger
            names[dIndex] = dObj.CustomerName;
            adds[dIndex] = dObj.CustomerAddress;
            if (dIndex == data.length - 1) bdGEO();
        });       
      
    }
    function search(start, end, route, passPoint) {
        var driving = new BMap.DrivingRoute(map, { renderOptions: { map: map, autoViewport: true }, policy: route });
        driving.search(start, end, { waypoints: passPoint });//waypoints表示途经点
    }

    function bdGEO() {
        var add = adds[index];
        geocodeSearch(add);
        index++;
    }

    //通过地址解析成经纬度
    function geocodeSearch(add) {
        if (index < adds.length) {
            setTimeout(window.bdGEO, 400);
        } 

        myGeo.getPoint(add, function (point) {
            debugger
            if (point) {
                if (gindex == adds.length - 1) {
                    end = new BMap.Point(point.lng, point.lat);
                    //三种驾车策略：最少时间，最短距离，避开高速
                    var routePolicy = [BMAP_DRIVING_POLICY_LEAST_TIME, BMAP_DRIVING_POLICY_LEAST_DISTANCE, BMAP_DRIVING_POLICY_AVOID_HIGHWAYS];

                    //var i = $("#driving_way select").val();
                    search(start, end, routePolicy[0], passPoint);
                    //设置信息框
                    for (var ii = 0; ii <= passPoint.length; ii++) {
                        var poi; var name ="客户姓名："+ names[ii];
                        if (ii == passPoint.length) poi = end;
                        else poi = passPoint[ii];
                        var opts = {
                            position: poi,    // 指定文本标注所在的地理位置
                            offset: new BMap.Size(30, -30)    //设置文本偏移量
                        }
                        var label = new BMap.Label(name, opts);  // 创建文本标注对象
                        label.setStyle({
                            color: "red",
                            fontSize: "12px",
                            height: "20px",
                            lineHeight: "20px",
                            fontFamily: "微软雅黑"
                        });
                        map.addOverlay(label);
                    }

                    return;
                }
                else passPoint[gindex] = new BMap.Point(point.lng, point.lat);  
                gindex++;
            }
        });
    }

    //获取url中的参数
    function  getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); // 匹配目标参数
        var result = window.location.search.substr(1).match(reg);  // 对querystring匹配目标参数
        if (result != null) {
            return decodeURIComponent(result[2]);
        } else {
            return null;
        }
    }
   
</script>
